<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }


        .box {
            padding: 5px;
        }

        .item {
            display: flex;
            padding: 10px;
            box-shadow: 1px 1px rgba(0, 0, 0, 0.1);
            margin: 5px 0px;
        }

        .item img {
            width: 104px;
            height: 78px;
            border-radius: 5px;
        }

        .item h3 {
            font-size: 16px;
            font-weight: 500;
            height: 40px;
            line-height: 20px;
            overflow: hidden;
        }

        .left div span {
            font-size: 12px;
        }

        .left div {
            margin-top: 10px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="item">
            <div class="left">
                <h3>一抓一放、张手就来：华为Mate 70／X6
                    系列手机、MatePad Pro 13.2 英寸平板 2..</h3>
                <div>
                    <span>新京报 2024-12-7 09:17:02</span>
                    <span>94评</span>
                </div>
            </div>
            <img src="https://img.ithome.com/newsuploadfiles/thumbnail/2024/11/812592_240.jpg" alt="">
        </div>
    </div>

    <script src="./axios.js"></script>
    <script>

        axios({
            url: 'https://hmajax.itheima.net/api/news'
        })
            .then(res => {

                console.log(res.data)  // 打印确认数据类型

                let newList = res.data.data   // 获取数据中的data
                let str = ``     // 定义空数组接收遍历后的数据

                // 遍历新获取的数据
                newList.forEach(item => {

                    str += `
                    <div class="item">
                        <div class="left">
                        <h3>${item.title}</h3>
                        <div>
                            <span>${item.source} ${item.time}</span>
                            <span>${item.cmtcount}评</span>
                        </div>
                    </div>
                    <img src="${item.img}">
                    </div>
                    
                    `
                })

                // 遍历好的数据插入页面
                document.querySelector('.box').innerHTML = str
            })

    </script>
</body>

</html>